<?php
	
require_once 'classes/Base/BaseDepartments.class.php';
require_once 'classes/Departments.class.php';

// display( $_REQUEST );
// display( $_POST ); exit;

if( false == isset( $_POST['Submit'] ) || false == valStr( $_POST['name'] ) ) { 
	$str = '';
	$str .= '<form name=myform method=post action=modules.php?file=department&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
	$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
	echo $str;
	exit;
}

if( true == isset( $_POST['operation'] ) && 'insert' == strtolower( $_POST['operation'] ) ) {
	
	$objDepartment = new BaseDepartments();
	
	$objDepartment->setName( $_POST['name'] );
	$objDepartment->setDescription( $_POST['description'] );
	$objDepartment->setDepartmentTypeId( $_POST['department_type_id'] );
	$objDepartment->setParantDepartmentId( ( true == valStr( $_POST['parant_department_id'] ) ) ? $_POST['parant_department_id'] : 'NULL' );
	$objDepartment->setDepartmentHeadName( ( true == valStr( $_POST['department_head_name'] ) ) ? $_POST['department_head_name'] : 'NULL' );
	$objDepartment->setDepartmentHeadId( ( true == valStr( $_POST['department_head_id'] ) ) ? $_POST['department_head_id'] : 'NULL' );
	$objDepartment->setOfficeNumber( $_POST['office_number'] );
	$objDepartment->setEmployeeCount( $_POST['employee_count'] );
	$objDepartment->setAddress( $_POST['address'] );
	$objDepartment->setContactPerson( $_POST['contact_person'] );
	$objDepartment->setCreatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objDepartment->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objDepartment->setUpdatedOn( 'now()' );
	
	if( 1 == $objDepartment->insert() ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=view_department&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
} else if( true == isset( $_POST['operation'] ) && 'update' == strtolower( $_POST['operation'] ) ) {
	$objDepartments 	= new Departments();
	$objDepartment 	= $objDepartments->fetchDepartmentsById( $_POST['department_id'] );
	
	$objDepartment->setId( $_POST['department_id'] );
	
	if( true == valStr( $_POST['name'] ) ) {
		$objDepartment->setName( $_POST['name'] );
	}
	
	if( true == valStr( $_POST['description'] ) ) {
		$objDepartment->setDescription( $_POST['description'] );
	}
	
	if( true == valStr( $_POST['department_type_id'] ) ) {
		$objDepartment->setDepartmentTypeId( $_POST['department_type_id'] );
	}
	
	if( true == valStr( $_POST['parant_department_id'] ) ) {
		$objDepartment->setParantDepartmentId( $_POST['parant_department_id'] );
	}
	
	if( true == valStr( $_POST['department_head_name'] ) ) {
		$objDepartment->setDepartmentHeadName( $_POST['department_head_name'] );
	}
	
	if( true == valStr( $_POST['department_head_id'] ) ) {
		$objDepartment->setDepartmentHeadId( $_POST['department_head_id'] );
	}
	
	if( true == valStr( $_POST['office_number'] ) ) {
		$objDepartment->setOfficeNumber( $_POST['office_number'] );
	}
	
	if( true == valStr( $_POST['employee_count'] ) ) {
		$objDepartment->setEmployeeCount( $_POST['employee_count'] );
	}
	
	if( true == valStr( $_POST['address'] ) ) {
		$objDepartment->setAddress( $_POST['address'] );
	}
	
	if( true == valStr( $_POST['contact_person'] ) ) {
		$objDepartment->setContactPerson( $_POST['contact_person'] );
	}
	
	$objDepartment->setUpdatedBy( ( true == isset( $_SESSION['SESS_MEMBER_ID'] ) ) ? $_SESSION['SESS_MEMBER_ID'] : 1 );
	$objDepartment->setUpdatedOn( 'now()' );

	
	if( 1 == $objDepartment->update() ) {
		$str = '';
		$str .= '<form name=myform method=post action=modules.php?file=view_department&type=view&mid=' . $_REQUEST['mid'] . '&smid=' . $_REQUEST['smid'] . '>';
		$str .= '<script language=JavaScript>document.myform.submit();</script></form>';
		echo $str;
		exit;
	}
}

?>